package com.association.main.frontcontroller;

import com.association.main.common.R;
import com.association.main.domain.Clubs;
import com.association.main.domain.JoinInfo;
import com.association.main.domain.Notifications;
import com.association.main.domain.Users;
import com.association.main.dto.NotificationsDto;
import com.association.main.service.ClubsService;
import com.association.main.service.JoinInfoService;
import com.association.main.service.NotificationsService;
import com.association.main.service.UsersService;
import com.association.main.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/front")
@Slf4j
public class NotificationController {
    @Autowired
    private NotificationsService notificationsService;

    @Autowired
    private ClubsService clubsService;


    @Autowired
    private UsersService usersService;


    @Autowired
    private JoinInfoService joinInfoService;
    /*
     * 首页分页查询
     * */
    @GetMapping("/notification/list")
    public R<Page> notificationsList(int page) {
        log.info(page+"");
        Page pageInfo = new Page(page, 10);
        LambdaQueryWrapper<Notifications> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(
                Notifications::getId,
                Notifications::getTitle,
                Notifications::getContent,
                Notifications::getCreatedAt,
                Notifications::getCreatedBy,
                Notifications::getClubId
                );
        lambdaQueryWrapper.eq(Notifications::getTurnUser, "true");
        lambdaQueryWrapper.orderByDesc(Notifications::getCreatedAt);
        //获取面向全部用户的
        List<Notifications> list = notificationsService.list(lambdaQueryWrapper);
        notificationsService.page(pageInfo, lambdaQueryWrapper);
        List<NotificationsDto> notificationsDtoList = list.stream().map((item) -> {
            Clubs clubs = clubsService.getById(item.getClubId());
            Users users = usersService.getById(item.getCreatedBy());
            NotificationsDto notificationsDto = new NotificationsDto(clubs.getName(), users.getName(), users.getEmail(), users.getPhone(),clubs.getLogo());
            BeanUtils.copyProperties(item, notificationsDto);
            return notificationsDto;
        }).collect(Collectors.toList());

        pageInfo.setRecords(notificationsDtoList);
        return R.success(pageInfo);
    }

    @GetMapping("/notification/getById")
    public R<NotificationsDto> notificationsById(int id) {
        Notifications notifications = notificationsService.getById(id);
        if (notifications == null) {
            return R.error("Notification not found");
        }
        Clubs clubs = clubsService.getById(notifications.getClubId());
        Users users = usersService.getById(notifications.getCreatedBy());
        NotificationsDto notificationsDto = new NotificationsDto(clubs.getName(), users.getName(), users.getEmail(), users.getPhone(),clubs.getLogo());
        BeanUtils.copyProperties(notifications, notificationsDto);
        return R.success(notificationsDto);
    }




    @GetMapping("/notification/mylist")
    public R<Page> myClubNotificationsList(int page, HttpServletRequest request) {
        Page<NotificationsDto> pageInfo = new Page<>(page, 10);
        String token = request.getHeader("token");
        Integer userId = JwtUtil.getUserId(token);

        // 构造查询条件
        LambdaQueryWrapper<JoinInfo> joinInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        joinInfoLambdaQueryWrapper.eq(JoinInfo::getMemberId, userId);

        // 获取用户参与列表
        List<JoinInfo> joinInfoList = joinInfoService.list(joinInfoLambdaQueryWrapper);
        if(joinInfoList.size()>0){
            List<Integer> joinClubIds = joinInfoList.stream().map(JoinInfo::getClubId).collect(Collectors.toList());

            // 构造查询条件
            LambdaQueryWrapper<Notifications> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Notifications::getClubId, joinClubIds);
            lambdaQueryWrapper.select(
                    Notifications::getId,
                    Notifications::getTitle,
                    Notifications::getContent,
                    Notifications::getCreatedAt,
                    Notifications::getCreatedBy,
                    Notifications::getClubId,
                    Notifications::getTurnUser
            );
            lambdaQueryWrapper.orderByDesc(Notifications::getCreatedAt);

            // 分页查询通知
            Page<Notifications> notificationPage = notificationsService.page(new Page<>(page, 10), lambdaQueryWrapper);

            // 将通知转换为通知 DTO 列表
            List<NotificationsDto> notificationsDtoList = notificationPage.getRecords().stream().map((item) -> {
                Clubs clubs = clubsService.getById(item.getClubId());
                Users users = usersService.getById(item.getCreatedBy());
                NotificationsDto notificationsDto = new NotificationsDto(clubs.getName(), users.getName(), users.getEmail(), users.getPhone(),clubs.getLogo());
                BeanUtils.copyProperties(item, notificationsDto);
                return notificationsDto;
            }).collect(Collectors.toList());

            // 将通知 DTO 列表放入 Page 中返回

            pageInfo.setRecords(notificationsDtoList);
            pageInfo.setTotal(notificationPage.getTotal());
        }

        return R.success(pageInfo);
    }



}
